package cn.jingzhuan.fm.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v4.content.res.ResourcesCompat
import android.support.v4.view.ViewCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutCompat
import android.support.v7.widget.LinearLayoutManager
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.ActivityFmAudioCommentBinding
import cn.jingzhuan.fm.databinding.ItemFmAudioCommentBinding
import cn.jingzhuan.fm.mvp.contract.FMAudioCommentContract
import cn.jingzhuan.fm.mvp.presenter.FMAudioCommentPresenter
import cn.jingzhuan.fm.pojo.FMAudioComment
import cn.jingzhuan.fm.ui.adapter.BaseBindingRecyclerAdapter
import cn.jingzhuan.fm.ui.adapter.BindingRecyclerViewHolder
import cn.jingzhuan.fm.ui.fragment.FMAudioCommentSendFragment
import com.jcodecraeer.xrecyclerview.ProgressStyle
import com.jcodecraeer.xrecyclerview.XRecyclerView
import javax.inject.Inject

class FMAudioCommentActivity : BaseBindingActivity<ActivityFmAudioCommentBinding>(),FMAudioCommentContract.View{


    companion object {
        var ARG_AUDIO_ID = "audioId"
        fun startActivity(context: Context,audioId : String) {
            var intent = Intent(context,FMAudioCommentActivity::class.java)
            intent.putExtra(ARG_AUDIO_ID,audioId)
            context.startActivity(intent)
        }
    }

    var commentList = mutableListOf<FMAudioComment>()
    lateinit var audioId:String
    @Inject
    lateinit var presenter : FMAudioCommentPresenter

    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityFmAudioCommentBinding?) {
        audioId =  intent.getStringExtra(ARG_AUDIO_ID)
        this.binding.viewToolbar?.title = "用户留言"
        setUpActionBar(this.binding.viewToolbar?.toolbar)
        initView()
        initData()
        initListener()
    }

    private fun initListener() {
        this.binding.sendCommentView.setOnClickListener {
            var fragment = FMAudioCommentSendFragment.newInstance(audioId)
            fragment.show(supportFragmentManager,"fmCommentSend")
        }
    }

    private fun initView() {
        ViewCompat.setElevation(this.binding.sendCommentView, 10f)

        val layoutManager = LinearLayoutManager(baseContext, LinearLayoutCompat.VERTICAL, false)
        this.mBinding.commentListRecycler.layoutManager = layoutManager

        this.mBinding.commentListRecycler.adapter = object: BaseBindingRecyclerAdapter<FMAudioComment>(baseContext, commentList){

            override fun getItemLayoutId(viewType: Int): Int  = R.layout.item_fm_audio_comment

            override fun bindData(holder: BindingRecyclerViewHolder, position: Int, item: FMAudioComment) {
                var commentBinding = holder.binding as ItemFmAudioCommentBinding
                commentBinding.comment = item
                if (item.reply != null) {
                    var interval = "主播："
                    var replay = "${item.reply.anchorName} $interval ${item.reply.content}"
                    var spannable = SpannableString(replay)
//                    builder.append("",ForegroundColorSpan(resources.getColor(R.color.blueGrey,theme)),Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
                    var end = replay.indexOf(interval) + interval.length
                    spannable.setSpan(ForegroundColorSpan(ResourcesCompat.getColor(resources,R.color.azul,theme)),0,end,Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
                    spannable.setSpan(ForegroundColorSpan(ResourcesCompat.getColor(resources,R.color.dark,theme)),end,replay.length,Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
                    commentBinding.reply.text = spannable
                    commentBinding.reply.visibility = View.VISIBLE
                } else {
                    commentBinding.reply.visibility = View.GONE
                }
                commentBinding.praise.setOnClickListener {
                    // 点赞事件
                }
            }
        }
        this.mBinding.commentListRecycler.emptyView = findViewById(R.id.view_empty_comment)
        val dividerItemDecoration = DividerItemDecoration(baseContext, DividerItemDecoration.VERTICAL)
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(baseContext!!, R.drawable.shape_divider)!!)
        this.mBinding.commentListRecycler.addItemDecoration(dividerItemDecoration)

        mBinding?.commentListRecycler?.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onLoadMore() {
                presenter.loadCommentList(audioId)
            }

            override fun onRefresh() {
                presenter.refresh(audioId)
            }
        })
        mBinding?.commentListRecycler?.setLoadingMoreEnabled(true)
        mBinding?.commentListRecycler?.defaultRefreshHeaderView?.setRefreshTimeVisible(true)
        mBinding?.commentListRecycler?.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin)
        mBinding?.commentListRecycler?.setLimitNumberToCallLoadMore(2)
    }

    private fun initData() {
        presenter.loadCommentList(audioId)
    }


    override fun onRefreshed() {
        commentList.clear()
        this.binding.commentListRecycler.refreshComplete()
    }

    override fun onDataChange(data: List<FMAudioComment>,hasMore:Boolean) {
        commentList.addAll(data)
        this.binding.commentListRecycler.loadMoreComplete()
        this.binding.commentListRecycler.adapter.notifyDataSetChanged()
    }

    override fun getLayoutId(): Int = R.layout.activity_fm_audio_comment

}